import { game } from "../../../../script/game/control/game";
import { assetsBase, companyBase, ItemBase } from "../../../../script/game/control/Item/ItemBase";
import { assetsType, itemType } from "../../../../script/game/gameType";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;
enum workType {
    "make" = 0,
    "sale" = 1,
}
//调动窗口
@ccclass
export default class NewClass extends WinBase {
    //员工操作界面
    count: number = 0; //当前的人数
    addCount: number = 0; //添加的人数
    cost = 0;
    maxCount: number = 0;
    item: companyBase = null;
    type: string = "0";
    start() {
        this.item = game.playerMgr.bag.companyBag.getItem(this._args.id);
        this.maxCount = this.item.calUnworkers(); //空闲人员
        
        this.nodes.curCount.getComponent(cc.Label).string = this.count + "";
        this.nodes.maxWorkers.getComponent(cc.Label).string = this.maxCount + "";
        this.touchEndEventScale(this.nodes.ensureBtn, () => {
            if(!this.item.costMoney(this.cost)) {
                ccs.winMgr.showWin("toast", {msg: "账户资金不足"});
                return ;
            }
            if(this._args.type == "make") {
                this.item.addMaker(this.addCount);
            } else if(this._args.type == "sale") {
                this.item.addSaleWorker(this.addCount);
            } else if(this._args.type == "hire"){
                this.item.addWorker(this.addCount);
            } else {
                this.item.delWorker(this.addCount);
            }
            this.updateData();
        })
        this.touchEndEventScale(this.nodes.hire, () => {
            this.type = "hire";
            this.initHire();
        })
        this.touchEndEventScale(this.nodes.unHire, () => {
            this.type = "unFire";
            this.initUnHire();
        })
        this.touchEndEventScale(this.nodes.make, () => {
            this.type = "make";
            this.initMake();
        })
        this.touchEndEventScale(this.nodes.sale, () => {
            this.type = "sale";
            this.initSale();
        })

    }
    initMake() {
        this.count = this.item.company.makeType.count; //当前人员
        this.maxCount = this.item.calMaxMake();
        this.nodes.hire.active = true;
        this.nodes.sale.active = true;
        this.nodes.unHire.active = false;
        this.nodes.make.active = false;
        this.updateData();
    }
    initSale() {
        this.count = this.item.company.salesType.count; //当前人员
        this.maxCount = this.item.calMaxSale();
        this.nodes.make.active = true;
        this.nodes.hire.active = true;
        this.nodes.sale.active = false;
        this.nodes.unHire.active = false;
        this.updateData();
    }
    initHire() {
        this.count = 0;//当前人员
        this.maxCount = this.item.calHireWorker();
        this.nodes.sale.active = false;
        this.nodes.hire.active = false;
        this.nodes.make.active = true;
        this.nodes.unHire.active = true;
        this.updateData();
    }
    initUnHire() {
        this.count = 0; //当前人员
        this.maxCount = this.item.calUnHireWorker();
        this.nodes.hire.active = true;
        this.nodes.make.active = true;
        this.nodes.unHire.active = false;
        this.updateData();
    }
    onSlider(tar, i) {
        let num = tar.progress;
        let tmp = this.count;
        if(this._args.type == "hire") {
            this.count = num * this.item.calHireWorker();
        } else if(this._args.type == "unHire"){
            this.count = num * this.item.calUnHireWorker();
        } else if(this._args.type == "make") {
            this.count = num * this.item.calMaxMake();
        } else {
            this.count = num * this.item.calMaxSale();
        }
        this.addCount = this.count - tmp;
        this.limitCount();
    }
    updateData() {
        this.nodes.curCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.count);
        this.nodes.maxWorkers.getComponent(cc.Label).string = "最大：" + ccs.gf.getOmitNum(this.maxCount);
        this.nodes.costCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost);
    }
    limitCount() {
        if(this.cost < 0 ) {
            this.cost = 0;
        }
        if(this.type == "hire" || this.type == "unHire") {
            this.cost = this.item.calHireMoney(this.addCount);
        }
        this.nodes.costCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost);
        this.nodes.curCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.count + this.addCount);

    }
}
